<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
    <title>用户编辑</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/libs/layui2.2.5/css/layui.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/common.css">
    <link rel="icon" href="href="${pageContext.request.contextPath}/static/libs/uiframe/frame/static/image/code.png">
    <form class="layui-form c3form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">用户名：</label>
            <div class="layui-input-block">
                <input type="text"
                <c:if test="${user.id != null}">
                    disabled="disabled"
                </c:if>
                       name="username" lay-verify="username" autocomplete="off" placeholder="请输入用户名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码：</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="pass"
                       placeholder="请输入6到12位密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">再次输入：</label>
            <div class="layui-input-block">
                <input type="password" name="passwordagin" lay-verify="pass"
                       placeholder="请再次输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号：</label>
            <div class="layui-input-block">
                <input type="text" name="phonenumber" lay-verify="phonenumber"
                       autocomplete="off" placeholder="请输入用户名" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="useradd">提 交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重 置</button>
            </div>
        </div>
    </form>

    <script type="text/javascript" src="${pageContext.request.contextPath}/static/libs/layui2.2.5/layui.js"></script>
    <script>
        var userid = '${user.id}',orgId = '${orgId}';

        layui.use(['form','layer'],function(){
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer;
            $("input[name='username']").val('${user.username}');

            //自定义验证规则
            form.verify({
                username: function(value){
                    if(value.length < 5){
                        return '用户名至少得5个字符啊';
                    }
                },
                pass: [/(.+){6,12}$/, '密码必须6到12位'],
                phonenumber: function(value){
                    if(value.length != 11){
                        return '手机号不对';
                    }
                }
            });

            //监听提交
            form.on('submit(useradd)', function(data){
                var formData = data.field,url;
                if(formData['password'] != formData['passwordagin']){
                    layer.msg('两次密码输入不一致', {icon: 5,anim: 6});
                    return false;
                }
                if(userid == ""){
                    url = '${pageContext.request.contextPath}/sys/user/usercreate';
                }else{
                    url = '${pageContext.request.contextPath}/sys/user/userupdate';
                }
                $.ajax({
                    type:'post',
                    dataType:'json',
                    url:url,
                    data:{
                        id: userid
                        , username: formData['username']
                        , password: formData['password']
                        , phonenumber: formData['phonenumber']
                        , orgId: orgId
                    },
                    success:function (data) {
                        layer.msg(data.status);
                        window.parent.layIndex = null;
                        window.parent.closeAllLayer();
                    },
                    error:function (error) {
                        console.log(error);
                    }
                });
                return false;
            });
        })
    </script>
</head>
<body>

</body>
</html>
